package dev.android.yeqh.alarmclock.db;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import dev.android.yeqh.alarmclock.model.Alarm;
import dev.android.yeqh.alarmclock.model.AlarmType;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.UrlQuerySanitizer.ValueSanitizer;

/**
 * CopyRight(c)2010-2010:    All right Reserved</br>
 * FileName:    DBM.java</br>
 * @author        yeqh
 * @version      1.0
 */
public class DBM {
	private SQLiteDatabase _db; 
	private DbHelper dbHelper; 
	private Context context; 
	
	public static final String DATABASE_NAME = "AlarmDB";
    public static final String TABLE_NAME = "Alarms";
    public static final int DATABASE_VERSION = 2; 
    
    private final static String KEY_ID = "_id";
    private final static String KEY_ALARM_TIME = "alarmTime";        
    private final static String KEY_ALARM_DESCRIPTION = "alarmDescription";
    private final static String KEY_REPEAT_TYPE = "repeatType";
    private final static String KEY_REPEAT_TIME = "repeatTime";

    private final int COLUMN_ID = 0;              
    private final int COLUMN_ALARM_TIME = 1;   
    private final int COLUMN_ALARM_DESCRIPTION = 2;     
    private final int COLUMN_REPEAT_TYPE = 3; 
    private final int COLUMN_REPEAT_TIME = 4;
    
    public static String DATEBASE_CREATE = "create table "+TABLE_NAME+" (" +
            KEY_ID + " INTEGER PRIMARY KEY autoincrement," +
            KEY_ALARM_TIME + " varchar(10)," +
            KEY_ALARM_DESCRIPTION + " varchar(200)," +
            KEY_REPEAT_TYPE + " varchar(20)," +
            KEY_REPEAT_TIME + " INTEGER" +
            ");";

	public DBM(Context context){
	    this.context = context;
		dbHelper = new DbHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	public void openDatabase(){
	    _db = dbHelper.getWritableDatabase();
	}

    public synchronized Alarm queryAlarmById(long alarmId){
    	Alarm alarm = null;
        
        openDatabase();
        String[] queryColumns = new String[] {KEY_ID, KEY_ALARM_TIME, KEY_ALARM_DESCRIPTION
                , KEY_REPEAT_TYPE, KEY_REPEAT_TIME};
        Cursor cusor = _db.query(TABLE_NAME, queryColumns, KEY_ID + " = '" + alarmId + "'", null, null, null, null);
        if (cusor.moveToFirst()) {
        	alarm = new Alarm();
        	alarm.setID(cusor.getLong(COLUMN_ID));
        	alarm.setAlarmTime(cusor.getLong(COLUMN_ALARM_TIME));
        	alarm.setAlarmDescription(cusor.getString(COLUMN_ALARM_DESCRIPTION));
        	alarm.setRepeatType(AlarmType.valueOf(cusor.getString(COLUMN_REPEAT_TYPE)));
        	alarm.setRepeatTime(cusor.getInt(COLUMN_REPEAT_TIME));
        }
        cusor.close();
        close();
        return alarm;
    }
    
    public synchronized List<Alarm> queryAlarms(){
    	List<Alarm> alarmList = new ArrayList<Alarm>();
    	Alarm alarm = null;
        
        openDatabase();
        String[] queryColumns = new String[] {KEY_ID, KEY_ALARM_TIME, KEY_ALARM_DESCRIPTION
                , KEY_REPEAT_TYPE, KEY_REPEAT_TIME};
        Cursor cusor = _db.query(TABLE_NAME, queryColumns, null, null, null, null, null);
        if (cusor.moveToFirst()) {
        	do {
	        	alarm = new Alarm();
	        	alarm.setID(cusor.getLong(COLUMN_ID));
	        	alarm.setAlarmTime(cusor.getLong(COLUMN_ALARM_TIME));
	        	alarm.setAlarmDescription(cusor.getString(COLUMN_ALARM_DESCRIPTION));
	        	alarm.setRepeatType(AlarmType.valueOf(cusor.getString(COLUMN_REPEAT_TYPE)));
	        	alarm.setRepeatTime(cusor.getInt(COLUMN_REPEAT_TIME));
	        	
	        	alarmList.add(alarm);
        	} while (cusor.moveToNext());
        }
        cusor.close();
        close();
        return alarmList;
    }
	
	public synchronized long insert(long alarmTime, String alarmDescription, AlarmType alarmType, long repeatTime) {
	    openDatabase();
	    
	    ContentValues contentValue = new ContentValues();
	    contentValue.put(KEY_ALARM_TIME, alarmTime);
	    contentValue.put(KEY_ALARM_DESCRIPTION, alarmDescription);
	    contentValue.put(KEY_REPEAT_TYPE, alarmType.name());
	    contentValue.put(KEY_REPEAT_TIME, repeatTime);
	    
		long result = _db.insert(TABLE_NAME, null, contentValue);
		
		close();
		return result;
	}
	
	public synchronized boolean removeById(long alarmId) {
	    openDatabase();
	    
	    boolean result = _db.delete(TABLE_NAME, KEY_ID + " = " + alarmId + "", null) > 0;
	    
	    close();
	    return result;
	}
	
	public synchronized boolean removeAll() {
	    openDatabase();
	    
	    boolean result = _db.delete(TABLE_NAME, null, null) > 0;
	    
	    close();
	    return result;
	}

	public synchronized long update(long alarmId, long alarmTime, String alarmDescription, AlarmType alarmType, long repeatTime) {
	    openDatabase();
	    
	    ContentValues contentValue = new ContentValues();
	    contentValue.put(KEY_ALARM_TIME, alarmTime);
	    contentValue.put(KEY_ALARM_DESCRIPTION, alarmDescription);
	    contentValue.put(KEY_REPEAT_TYPE, alarmType.name());
	    contentValue.put(KEY_REPEAT_TIME, repeatTime);
        int result = _db.update(TABLE_NAME, contentValue, KEY_ID + " = " + alarmId, null);
        
        close();
        return result;
	}

	public void close(){
		_db.close();
	}
	
}
